package com.tutorial.webservices.saaj.utils;

import java.util.Enumeration;
import java.util.Iterator;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.soap.MimeHeader;
import javax.xml.soap.MimeHeaders;

public class HeadersParser {
    
    @SuppressWarnings("unchecked")
    public MimeHeaders getMimeHeaders(HttpServletRequest request) {
	if (request == null) {
	    new RuntimeException("HttpServletRequest is null"); //$NON-NLS-1$
	}
	MimeHeaders mimeHeader = new MimeHeaders();
	Enumeration enumHeaders = request.getHeaderNames();
	while (enumHeaders.hasMoreElements()) {
	    String enumHeader = (String) enumHeaders.nextElement();
	    mimeHeader.addHeader(enumHeader, request.getHeader(enumHeader));
	}
	return mimeHeader;
    }

    @SuppressWarnings("unchecked")
    public ServletResponse putResponseHeaders(final MimeHeaders headers,
	    final HttpServletResponse response) {
	Iterator iter = headers.getAllHeaders();
	while (iter.hasNext()) {
	    MimeHeader mimeHeader = (MimeHeader) iter.next();
	    response.addHeader(mimeHeader.getName(), mimeHeader.getValue());
	}
	return response;
    }

    @SuppressWarnings("unchecked")
    public void printHeaders(HttpServletRequest request) {
	if (request == null) {
	    new RuntimeException("HttpServletRequest is null"); //$NON-NLS-1$
	}
	Enumeration enumHeaders = request.getHeaderNames();
	while (enumHeaders.hasMoreElements()) {
	    String enumHeader = (String) enumHeaders.nextElement();
	    System.out.print(enumHeader);
	    System.out.print(" : "); //$NON-NLS-1$
	    System.out.print(request.getHeader(enumHeader));
	    System.out.println();
	}

    }
}
